﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Models;
using Common;
using MyVideo;


namespace LibraryManagerPro
{
    public partial class FrmAddBook : Form
    {
        private BookManager objBookManager = new BookManager();
        private Video objVideo = null;
        private List<Book> bookList = new List<Book>();
        public FrmAddBook()
        {
            InitializeComponent();
            //初始化图书分类下拉框
            this.cboBookCategory.DataSource = objBookManager.GetAllCategory();
            this.cboBookCategory.DisplayMember = "CategoryName";
            this.cboBookCategory.ValueMember = "CategoryId";
            this.cboBookCategory.SelectedIndex = -1;

            //初始化出版社
            this.cboPublisher.DataSource = objBookManager.GetAllPublisher();
            this.cboPublisher.DisplayMember = "PublisherName";
            this.cboPublisher.ValueMember = "PublisherId";
            this.cboPublisher.SelectedIndex = -1;

            //禁用摄像头操作相关类
            this.btnCloseVideo.Enabled = false;
            this.btnTake.Enabled = false;

            //禁止数据列表控件自动生成列
            this.dgvBookList.AutoGenerateColumns = false;

        }
        //启动摄像头
        private void btnStartVideo_Click(object sender, EventArgs e)
        {
            try
            {
                //创建摄像头操作类
                this.objVideo = new Video(this.pbImage.Handle, this.pbImage.Left,
                    this.pbImage.Top, this.pbImage.Width, (short)this.pbImage.Height);
                //打开摄像头
                objVideo.OpenVideo();
                //禁用相关类
                this.btnStartVideo.Enabled = false;
                this.btnCloseVideo.Enabled = true;
                this.btnTake.Enabled = true;

            }
            catch (Exception ex)
            {

                MessageBox.Show("摄像头启动失败" + ex.Message, "提示信息");
            }
        }
        private void btnCloseVideo_Click(object sender, EventArgs e)
        {
            this.objVideo.CloseVideo();
            this.btnStartVideo.Enabled = true;
            this.btnCloseVideo.Enabled = false;
            this.btnTake.Enabled = false;

        }
        //开始拍照
        private void btnTake_Click(object sender, EventArgs e)
        {
            this.pbCurrentImage.Image = this.objVideo.CatchVideo();

        }
        //选择图片
        private void btnChoseImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog objOpenFile = new OpenFileDialog();
            DialogResult result = objOpenFile.ShowDialog();
            if (result == DialogResult.OK)
            {
                if (DataValidate.IsImage(objOpenFile.FileName.ToString()) == true)
                {
                    this.pbCurrentImage.Image = Image.FromFile(objOpenFile.FileName);
                }
                else
                {
                    MessageBox.Show("请选择图片文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

            }
        }
        //清除   
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.pbCurrentImage.Image = null;
        }
        //判断图书条码是否已经存在
        private void txtBarCode_Leave(object sender, EventArgs e)
        {
            if (this.txtBarCode.Text.Trim().Length > 0)
            {
                if (objBookManager.BarCodeExisted(txtBarCode.Text.Trim()))
                {
                    MessageBox.Show("改条码已经存在", "提示信息");
                    this.txtBarCode.SelectAll();
                    this.txtBarCode.Focus();
                }
            }
        }
        private void txtBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                txtBarCode_Leave(null, null);
            }
        }
        //确认添加
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.txtBookName.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入图书信息", "验证信息");
                this.txtBookName.Focus();
                return;
            }
            if (this.cboBookCategory.SelectedIndex == -1)
            {
                MessageBox.Show("请选择图书分类", "验证信息");
                return;
            }
            if (this.cboPublisher.SelectedIndex == -1)
            {
                MessageBox.Show("请选择出版社", "验证信息");
                return;
            }
            if (this.txtBarCode.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入条码", "验证信息");
                this.txtBookName.Focus();
                return;
            }
            if (this.pbCurrentImage.Image == null)
            {
                MessageBox.Show("选择图书封面", "验证信息");
                return;
            }
            Book objBook = new Book()
            {
                BookName = this.txtBookName.Text.Trim(),
                BookCategory = Convert.ToInt32(this.cboBookCategory.SelectedValue),
                PublisherId = Convert.ToInt32(this.cboPublisher.SelectedValue),
                PublishDate = Convert.ToDateTime(this.dtpPublishDate.Text),
                Author = this.txtAuthor.Text.Trim(),
                UnitPrice = Convert.ToDouble(this.txtUnitPrice.Text.Trim()),
                BarCode = this.txtBarCode.Text.Trim(),
                BookCount = Convert.ToInt32(this.txtBookCount.Text.Trim()),
                Remainder = Convert.ToInt32(this.txtBookCount.Text.Trim()),
                BookPosition = this.txtBookPosition.Text.Trim(),
                BookImage = new Common.SerializeObjectToString().SerializeObject(this.pbCurrentImage.Image),
                PublisherName=this.cboPublisher.Text
            };

            try
            {
                objBookManager.AddBook(objBook);
                //列表中同步显示添加的对象
                this.bookList.Add(objBook);
                this.dgvBookList.DataSource = null;
                this.dgvBookList.DataSource = this.bookList;
                foreach (Control item in gbBook.Controls)
                {
                    if (item is TextBox) item.Text = "";
                    else if(item is ComboBox)
                    {
                        ((ComboBox)item).SelectedIndex = -1;
                    }
                }
                this.pbCurrentImage.Image = null;
                this.txtBookName.Focus();
            }
            catch (Exception ex)
            {
                
                MessageBox.Show("添加出现异常"+ex.Message,"错误提示");
            }
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvBookList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Common.DataGridViewStyle.DgvRowPostPaint(this.dgvBookList, e);
        }

    }
}
